package com.qcby.llmtchstd.controller;

import com.qcby.llmtchstd.common.ResponseResult;
import com.qcby.llmtchstd.dao.TeachingPlanDao;
import com.qcby.llmtchstd.entity.TeachingPlan;
import com.qcby.llmtchstd.service.TeachingPlanService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/teaching-plans")
@Api(tags = "教案管理")
public class TeachingPlanController {
    @Autowired
    private TeachingPlanService teachingPlanService;

    @GetMapping("/list")
    public ResponseResult<List<TeachingPlan>> listPlans() {
        try {
            List<TeachingPlan> plans = teachingPlanService.listAll();
            return ResponseResult.success("查询成功", plans);
        } catch (Exception e) {
            return ResponseResult.fail("500", "服务器内部错误");
        }
    }

    // 搜索教案
    @PostMapping("/search-one")
    public ResponseResult<TeachingPlan> selectThis(@RequestBody TeachingPlan teachingPlan) {
        String name = teachingPlan.getName();
        TeachingPlan ans = teachingPlanService.selectThis(name);
        if(ans != null) {
            return ResponseResult.success("查询成功", ans);
        } else {
            return ResponseResult.fail("5001", "查询失败，请重试");
        }
    }

    // 删除教案
    @DeleteMapping("/delete/{id}")
    public ResponseResult<Integer> deleteThis(@PathVariable Integer id) {
        Integer ans = teachingPlanService.deleteThis(id);
        if(ans != 0) {
            return ResponseResult.success("删除成功", ans);
        } else {
            return ResponseResult.fail("5001", "删除失败，请重试");
        }
    }

    // 创建教案
    @PostMapping("/create")
    public ResponseResult<TeachingPlan> createPlan(@RequestBody TeachingPlan plan, HttpServletRequest request) {
        // 设置创建人ID为当前登录教师
        Integer teacherId = (Integer) request.getSession().getAttribute("teacherId");
        plan.setTeacherId(teacherId);
        TeachingPlan createdPlan = teachingPlanService.createPlan(plan);
        return ResponseResult.success("创建成功", createdPlan);
    }

    // 预览教案
    @GetMapping("/preview/{id}")
    public ResponseResult<TeachingPlan> previewPlan(@PathVariable Integer id) {
        TeachingPlan plan = teachingPlanService.selectById(id);
        if(plan != null) {
            return ResponseResult.success("查询成功", plan);
        } else {
            return ResponseResult.fail("5001", "教案不存在");
        }
    }

    // 分页查询教案
    @PostMapping("/page")
    public ResponseResult<List<TeachingPlan>> listPlansByPage(@RequestBody Map<String, Integer> params) {
        try {
            int offset = params.get("offset");
            int pageSize = params.get("pageSize");
            int id=params.get("id");
            // 这里需要调用对应的Service方法实现分页查询
            List<TeachingPlan> plans = teachingPlanService.selectPage(offset, pageSize,id);
            return ResponseResult.success("查询成功", plans);
        } catch (Exception e) {
            return ResponseResult.fail("500", "服务器内部错误");
        }
    }


    @PostMapping("/search")
    public ResponseResult<List<TeachingPlan>> searchPlans(@RequestBody Map<String, String> params) {
        String name = params.get("name");
        int id=Integer.parseInt(params.get("id"));
        List<TeachingPlan> plans = teachingPlanService.searchByName(name,id);
        return ResponseResult.success("查询成功", plans);
    }

    @PostMapping("/count")
    @ResponseBody
    @ApiOperation("获取数量" )
    public ResponseResult<Integer> countPlans(@RequestBody Integer id) {
        try {
            int count = teachingPlanService.selectCount(id);
            return ResponseResult.success("查询成功", count);
        } catch (Exception e) {
            return ResponseResult.fail("500", "服务器内部错误");
        }
    }

    @PostMapping("/search-count")
    @ResponseBody
    @ApiOperation("根据教师获取搜索后的数量" )
    public ResponseResult<Integer> countSer(@RequestBody Map<String, String> params) {
        try {
            String name = params.get("name");
            int id=Integer.parseInt(params.get("id"));
            int count = teachingPlanService.countSer(name,id);
            return ResponseResult.success("查询成功", count);
        } catch (Exception e) {
            return ResponseResult.fail("500", "服务器内部错误");
        }
    }
}

